package com.hanson.service;

import com.hanson.common.exception.BusinessException;
import com.hanson.common.util.PhoneUtils;
import com.hanson.entity.UserAddresBean;
import com.hanson.mapper.UserAddresBeanMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserAddressService {
    @Autowired
    private UserAddresBeanMapper userAddresBeanMapper;

    @Transactional(rollbackFor = Exception.class)
    public void addAddress(String openId, UserAddresBean userAddresBean) {
        checkAddress(userAddresBean);
        if ("1".equals(userAddresBean.getIsDefault())) {
            userAddresBeanMapper.updateIsDefaultByOpenId(0, openId);
        }
        int count = userAddresBeanMapper.countByOpenIdAndIsdefault(openId, 1);
        if (count == 0) {
            userAddresBean.setIsDefault(1);
        }
        if (StringUtils.isNotEmpty(userAddresBean.getAddress())) {
            String[] addressArr = userAddresBean.getAddress().split("\\s+");
            userAddresBean.setProvince(addressArr[0]);
            userAddresBean.setCity(addressArr[1]);
            userAddresBean.setDistrict(addressArr[2]);
        }
        userAddresBean.setCreateDate(new Date());
        userAddresBean.setOpenId(openId);
        userAddresBeanMapper.insert(userAddresBean);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateAddress(String openId, UserAddresBean userAddresBean) {
        checkAddress(userAddresBean);
        UserAddresBean returnBean = userAddresBeanMapper.selectByPrimaryKey(userAddresBean.getId());
        if (null == userAddresBean || !openId.equals(returnBean.getOpenId())) {
            throw new BusinessException("参数不正确");
        }
        userAddresBean.setCreateDate(returnBean.getCreateDate());
        userAddresBean.setOpenId(openId);
        if ("1".equals(userAddresBean.getIsDefault())) {
            userAddresBeanMapper.updateIsDefaultByOpenId(0, openId);
        }
        int count = userAddresBeanMapper.countByOpenIdAndIsdefault(openId, 1);
        if (count == 0) {
            userAddresBean.setIsDefault(1);
        }
        if (StringUtils.isNotEmpty(userAddresBean.getAddress())) {
            String[] addressArr = userAddresBean.getAddress().split("\\s+");
            userAddresBean.setProvince(addressArr[0]);
            userAddresBean.setCity(addressArr[1]);
            userAddresBean.setDistrict(addressArr[2]);
        }
        userAddresBean.setUpdateDate(new Date());
        userAddresBeanMapper.updateByPrimaryKey(userAddresBean);
    }

    /**
     * 校验地址信息
     *
     * @param userAddresBean
     */
    private void checkAddress(UserAddresBean userAddresBean) {
        if (StringUtils.isEmpty(userAddresBean.getPhone())) {
            throw new BusinessException("手机号为空");
        }
        if (!PhoneUtils.isPhoneLegal(userAddresBean.getPhone())) {
            throw new BusinessException("手机号格式不正确");
        }
        if (StringUtils.isEmpty(userAddresBean.getAddress())) {
            throw new BusinessException("地址详情为空");
        }
    }

    public List<UserAddresBean> selectByOpenId(String openId) {
        List<UserAddresBean> list = userAddresBeanMapper.selectByOpenId(openId);
        if (CollectionUtils.isNotEmpty(list)) {
            list.stream().forEach(c -> {
                c.setAddress(c.getProvince() + c.getCity() + c.getDistrict() + c.getDetail());
            });
        }
        return list;
    }

    public UserAddresBean selectById(String id) {
        UserAddresBean userAddresBean = userAddresBeanMapper.selectByPrimaryKey(Integer.parseInt(id));
        if (null != userAddresBean) {
            userAddresBean.setAddress(userAddresBean.getProvince() + " " + userAddresBean.getCity() + " " + userAddresBean.getDistrict());
        }
        return userAddresBean;
    }


    public void deleteAddress(String id) {
        userAddresBeanMapper.deleteByPrimaryKey(Integer.parseInt(id));
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateIsDefault(String openId, String isDefault, String id) {
        if ("1".equals(isDefault)) {
            userAddresBeanMapper.updateIsDefaultByOpenId(0, openId);
        }
        userAddresBeanMapper.updateIsDefault(Integer.parseInt(isDefault), Integer.parseInt(id));
    }
}
